/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.foundation.jala.jamerzip.modelzip;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.foundation.jala.jamerzip.components.Jamer;
import org.foundation.jala.jamerzip.components.JamerElement;
import org.foundation.jala.jamerzip.view.InformationView;
import org.foundation.jala.jamerzip.view.MainView;

public class ZipManagerImpl implements ZipManager {

	private Jamer manager;
	
	private MainView view;
	private NewZip newZip;
	private List<NavigateController> zipList;
	private InformationView infoView;

	public ZipManagerImpl(Jamer manager) {
		this.manager = manager;
		this.zipList = new ArrayList<NavigateController>();
		this.view = new MainView();
		this.view.setActions(this);
		this.view.setVisible(true);
		this.newZip = new NewZip(view);
		this.infoView = new InformationView(view);
	}

	@Override
	public void createNewZip() {
		this.newZip.showView();
	}

	@Override
	public void showProperties() {
		int index = this.view.getSelectTab();
		if(index>=0&&(index<this.zipList.size())){
			int row  = this.zipList.get(index).getSelectedFileElement();
			JamerElement element = this.zipList.get(index).getFileElement(row);
			this.infoView.fillData(element);	
		}else{
			JOptionPane.showMessageDialog(null, "There is not a selected element");
		}
	}

	private String showFileChooser(String tittle) {
		String selected = null;
		JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
		fc.setDialogTitle(tittle);
		fc.setMultiSelectionEnabled(true);
		int result = fc.showSaveDialog(new JFrame());
		if (result == JFileChooser.APPROVE_OPTION) {
			File path = fc.getSelectedFile();
			selected = path.getAbsolutePath();
		}
		return selected;
	}

	@Override
	public void extractContent() {
		int tabSelected = this.view.getSelectTab();
		if(tabSelected>=0){
			String source = this.zipList.get(tabSelected).getName();
			System.out.println("Source = "+source);
		}else{
			JOptionPane.showMessageDialog(null, "There is not a selected element");
		}
	}

	@Override
	public void openZip() {
		String path = showFileChooser("Navigate zip");
		if (path != null) {
			boolean zipIsRunning = verifyZipRunning(path);
			if (!zipIsRunning) {
				JamerElement[] directories = this.manager.doReadZipToNavigate(path);
				NavigateController newNavigable = new NavigateController(path,directories);
				this.zipList.add(newNavigable);
				this.view.addToTabbed(path, newNavigable.getView());
			}else{
				JOptionPane.showMessageDialog(null, "This element is currently being used");
			}
		}
	}

	private boolean verifyZipRunning(String absolutePath) {
		boolean exist = false;
		int i = 0;
		while(i<this.zipList.size()&&!exist){
			exist = this.zipList.get(i++).getName().equals(absolutePath);
		}
		return exist;
	}

	@Override
	public void remove(int index) {
		this.zipList.remove(index);
	}
}
